<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">订单编号</label>
        <el-input
          v-model="query.orderSn"
          clearable
          placeholder="订单编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">订单状态</label>
        <el-select v-model="query.orderStatus" placeholder="请选择订单状态">
          <el-option
            v-for="item in dict.order_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        max-height="300"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderSn" label="订单编号" />
        <el-table-column prop="userId" label="会员ID" />
        <el-table-column prop="receiverId" label="地址ID" />
        <el-table-column prop="totalPrice" label="订单总金额" />
        <el-table-column prop="payPrice" label="支付金额" />
        <el-table-column prop="orderStatus" label="订单状态">
          <template slot-scope="scope">
            {{ dict.label.order_status[scope.row.orderStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" />
        <el-table-column
          v-permission="['admin','ycOrder:edit','ycOrder:del']"
          fixed="right"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <!-- <udOperation
               :data="scope.row"
               :permission="permission"
             />-->
            <el-button size="mini" type="success" @click="assignOrder(scope.row)">分配订单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <div class="assign">
      <el-row>
        <el-col :span="12">
          <div class="assign-left">
            <div class="assign-left-top">
              <div class="assign-left-top-title">选择配货司机</div>
              <div class="assign-left-top-select">
                <el-select v-model="driver" clearable placeholder="请选择" @change="changeDriver">
                  <el-option
                    v-for="item in driverList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </div>
            </div>
            <div class="assign-left-table">
              <el-table
                :data="driverOrderList"
                border
                max-height="400"
                style="width: 80%"
              >
                <el-table-column prop="orderSn" label="订单编号" />
                <el-table-column prop="receiverId" label="地址ID" />
                <el-table-column prop="totalPrice" label="订单总金额" />
                <el-table-column
                  v-permission="['admin','ycOrder:edit','ycOrder:del']"
                  fixed="right"
                  label="操作"
                  width="150px"
                  align="center"
                >
                  <template slot-scope="scope">
                    <!-- <udOperation
                       :data="scope.row"
                       :permission="permission"
                     />-->
                    <el-button size="mini" type="danger" @click="assignOrder(scope.row)">撤销分配</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>

          </div>
        </el-col>
        <el-col :span="8">
          <div class="assign-right">
            <el-button size="mini" type="warning">打印分拣单</el-button>
            <el-button size="mini" type="warning">打印配送单</el-button>
            <view class="assign-right-top" />
          </div>
        </el-col>
      </el-row>
    </div>
  </div>

</template>

<script>
import crudYcOrder from '@/api/order/assignOrder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  orderSn: null,
  userId: null,
  receiverId: null,
  totalPrice: null,
  discountsPrice: null,
  payPrice: null,
  roundPrice: null,
  payTime: null,
  couponId: null,
  orderStatus: null,
  remarks: null,
  createTime: null,
  postage: null
}
export default {
  name: 'YcOrder',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['order_status'],
  cruds() {
    return CRUD({ title: '分单系统', url: 'api/order', sort: 'id,desc', crudMethod: { ...crudYcOrder }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'ycOrder:add'],
        edit: ['admin', 'ycOrder:edit'],
        del: ['admin', 'ycOrder:del']
      },
      rules: {
        userId: [
          { required: true, message: '会员ID不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'orderSn', display_name: '订单编号' },
        { key: 'userId', display_name: '会员ID' },
        { key: 'orderStatus', display_name: '订单状态' }
      ],
      driver: '',
      driverList: [{ name: 'dfsdfs', id: 0 }],
      driverOrderList: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.optShow = {
        add: false,
        edit: false,
        del: false,
        download: false
      }
      const query = this.query
      console.log(query)
      query.isDistribution = false
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    },
    /*     [CRUD.HOOK.beforeRefresh]() {
             return true
           },*/
    changeDriver(e) {
      const _t = this
      _t.driver = e
      console.log(e)
    },
    assignOrder(row) {
      const _t = this
      _t.crud.refresh()
      // 去重
      let _s = false
      if (_t.driverOrderList.length == 0) {
        _t.driverOrderList = _t.driverOrderList.concat(row)
      } else {
        for (let i = 0; i < _t.driverOrderList.length; i++) {
          if (row.id == _t.driverOrderList[i].id) {
            _s = true
          }
        }
        if (!_s) {
          _t.driverOrderList = _t.driverOrderList.concat(row)
        } else {
          this.$message.error('该订单已添加！')
        }
      }
      /* console.log(_t.driver)
        if (_t.driver == "") {
          this.$message.error('错误提示：请先选择司机再分配订单')
        } else {

        }*/
    }
  }
}
</script>

<style scoped>
  .app-container {
    margin-bottom: 100px;
    padding: 20px 20px 20px 20px;
  }

  .assign-left-top {
    display: flex;
    align-items: center;
    padding: 20px 0;
  }

  .assign-left-top-title {
    font-size: 16px;
    font-weight: bold;
    padding-right: 10px;
  }

  .assign-right {
    padding: 20px 0;
  }

  .assign-right-top {
    width: 100%;
    padding: 20px 0;
  }
</style>
